package src.percolation;
import java.util.Map;
import java.util.Random;
import java.util.TreeMap;

import org.apache.commons.math.stat.StatUtils;

import src.utils.Utils;

public class Pr1_a {

	public static void main(String[] argvs) throws Exception {
	
		int minNetSize;
		int maxNetSize;
		int increment;
		int sampleSize;
		
		double eps;
		
		String meanOutFile;
		String sigmaOutFile;
		
	try {

		minNetSize = new Integer(argvs[0]);
		maxNetSize = new Integer(argvs[1]);
		increment = new Integer(argvs[2]);
		sampleSize = new Integer(argvs[3]);
		
		eps = new Double(argvs[4]);
		
		meanOutFile = argvs[5];
		sigmaOutFile = argvs[6];
	
	} catch (Exception e) {
		showUsage();
		return;
	}
	
	Random gen = new Random();

	double[] results = new double[sampleSize];
	
	Map<Double,Double> dataMean = new TreeMap<Double,Double>();
	Map<Double,Double> dataSigma = new TreeMap<Double,Double>();
	
	for(int netSize = minNetSize; netSize<maxNetSize;netSize+=increment) {
		for(int i=0; i<sampleSize; i++) {
			long seed = gen.nextLong();
			Red myRed = new Red(netSize,seed);
			double pc = Utils.findPC(netSize,seed,eps);
			results[i] = pc;
			}
		double mean = StatUtils.mean(results);
		double sigma = Math.sqrt(StatUtils.variance(results));
		
		dataMean.put(new Double(netSize), mean);
		dataSigma.put(new Double(netSize), sigma);
		
	}
	Utils.printToFile(dataMean, meanOutFile);
	Utils.printToFile(dataSigma, sigmaOutFile);
	}

	private static void showUsage() {
		
		System.out.println("Usage: ");
		
		System.out.println("<minNetSize> <maxNetSize> <increment> <sampleSize> <eps> <meanOutFile> <sigmaOutFile>\n");
		
		System.out.println("<minNetSize>: minimun size of net.");
		System.out.println("<maxNetSize>: maximum size of net.");
		System.out.println("<increment>: increment in size of net.");
		System.out.println("<sampleSize>: number of trials to compute statistics on each size.");
		System.out.println("<eps>: required precision when bisecting.");
		System.out.println("<meanOutFile>: file with pc mean results.");
		System.out.println("<sigmaOutFile>: file with pc sigma results.");
	}
}
